package com.bw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.mapper.DeptMapper;
import com.bw.po.Dept;
import com.bw.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：lau
 * @Package：com.bw.service.impl
 * @Project：my-framework
 * @name：DeptServiceImpl
 * @Date：2025/11/3 13:58
 * @Filename：DeptServiceImpl
 */
@Service
public class DeptServiceImpl  extends ServiceImpl<DeptMapper, Dept> implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public Map getAllDepts() {
       /*
        // 使用mapper的形式去查询
       QueryWrapper<Dept> wrapper = new QueryWrapper<Dept>();
        return deptMapper.selectList(wrapper);*/

        // 使用Service的查询方式
        // 分页查询
        // 1.分页查询，new Page()的两个参数分别是：页码、每页大小
        // 带有条件的查询
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<Dept>();
        wrapper.like(Dept::getName, "部");
        Page<Dept> p= this.page(new Page<>(2, 2),wrapper);
        // 2.总条数
        System.out.println("total = " + p.getTotal());
        // 3.总页数
        System.out.println("pages = " + p.getPages());
        List<Dept> records = p.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", p.getTotal());
        map.put("data", records);

        return map;

        // LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<Dept>();
        // wrapper.like(Dept::getName, "部");
        // return list(wrapper);
    }
}
